package com.huli.service;

import com.huli.exception.BusinessException;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ResourceBundle;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class YunPanServiceImp implements YunPanService{

    private ServerSocket serverSocket;
    private ResourceBundle bundle;
    private ExecutorService threadPool;

    @Override
    public void init() {
        // 读取配置文件
        bundle = ResourceBundle.getBundle("yunpan");
        int port = Integer.parseInt(bundle.getString("Port"));
        try {
            serverSocket = new ServerSocket(port);
        } catch (IOException e) {
            new BusinessException("端口号冲突,请重新定义");
        }
        threadPool = Executors.newFixedThreadPool(50);
    }

    @Override
    public void start() {
        while (true) {
            try {
                Socket socket = serverSocket.accept();
                threadPool.submit(new FileUpDownServiceImp(socket));
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
